﻿Imports Excepciones
Imports Negocio
Imports Servicios

Namespace My
    Partial Friend Class MyApplication

        Protected Overrides Function OnStartup(eventArgs As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) As Boolean

            Dim Splash As New SpScreen()
            Splash.Show()

            Try
                InitApp.Init()
            Catch ex As CannotOpenConnectionDataBaseException
                Application.HandlerException(ex)
            Catch ex As CustomException
                Application.HandlerException(ex)
                MsgBox("Ocurrió un error al iniciar la aplicación, la misma se cerrará", MsgBoxStyle.Critical)
                System.Environment.Exit(1)
            Catch ex As Exception
                Application.HandlerException(ex)
            End Try

            Dim auth As Boolean = False
            Do Until auth = True
                auth = formLogin()
            Loop

            Splash.Close()

            GesBitacoras.Guardar("ModPrincipal", "MSGB004", New String() {Current.Usuario.Nick})

            Return MyBase.OnStartup(eventArgs)
        End Function

        Private Function formLogin() As Boolean
            Dim ret As Boolean = False
            Dim frm As New LoginForm
            If frm.ShowDialog = DialogResult.OK Then
                Try
                    Seguridad.Seguridad.Login(frm.txtUsername.Text, frm.txtPassword.Text)
                    Current.Usuario = Servicios.GesUsuarios.Obtener(frm.txtUsername.Text)
                    Servicios.GesPermisos.CargarPermisos(Current.Usuario.Perfil)
                    ret = True
                Catch ex As UserNotFoundException
                    Application.HandlerException(ex)
                Catch ex As Exception
                    Application.HandlerException(ex)
                End Try
            Else
                End
            End If
            Return ret
        End Function

        ''' <summary>
        ''' Maneja las excepciones de la aplicación
        ''' </summary>
        ''' <param name="ex">Exception ocurrida</param>
        ''' <remarks></remarks>
        Public Sub HandlerException(ex As Exception)

            If TypeOf ex Is CustomException Then
                Dim cex As CustomException = DirectCast(ex, CustomException)
                Dim msg As Mensaje = Nothing
                Try
                    msg = GesIdiomas.TraduceMensaje(cex.Code)
                Catch ex1 As Exception
                    msg = New Mensaje()
                End Try

                If msg.Codigo = "" Then
                    msg = New Mensaje()
                    msg.Caption = cex.Message
                    msg.Codigo = cex.Code
                    GesBitacoras.Guardar(cex.Modulo, "MSGCONF001", New String() {cex.Code})
                End If

                MessageExt.Show(msg.Caption, cex.Type)
                GesBitacoras.Guardar(cex.Modulo, msg.Codigo, New String() {})

                If cex.Type = Windows.Forms.MessageBoxIcon.Error Then
                    MsgBox("Ocurrió un error crítico en la aplicación, la misma se cerrará", MsgBoxStyle.Critical)
                    System.Environment.Exit(1)
                End If
            Else
                MsgBox("Ocurrió un error no controlado en la aplicación, la misma se cerrará", MsgBoxStyle.Critical)
                System.Environment.Exit(1)
            End If
        End Sub


        ''' <summary>
        ''' Traduce todos los formularios abiertos
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub ReloadLanguage()

            For Each f As Form In Application.OpenForms
                If TypeOf f Is Base Then
                    Try
                        DirectCast(f, Base).Translate()
                    Catch ex As Exception

                    End Try
                End If
            Next

        End Sub

    End Class
End Namespace
